package com.jinunn.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import com.jinunn.hosp.repository.DepartmentRepository;
import com.jinunn.hosp.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : JinDun
 * @date : 2021/4/28 23:13
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> switchMap) {
        String jsonString = JSONObject.toJSONString(switchMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);
        //根据医院编号 和 科室编号查询
        Department departmentExist = departmentRepository.
                getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        if (departmentExist!=null){
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {
            department.setIsDeleted(0);
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        //withIgnoreCase 忽略大小写
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);

        return departmentRepository.findAll(example, pageable);
    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> getDeptTree(String hoscode) {
        //查询出所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        List<Department> all = departmentRepository.findAll(example);

        //根据BigCode分组
        Map<String, List<Department>> departmentMap = all.stream()
                .collect(Collectors.groupingBy(Department::getBigcode));

        //老师的写法
        //for (Map.Entry<String,List<Department>> entry:departmentMap.entrySet()){
        //    .......
        //}

        // iterator的写法
        //List<DepartmentVo> result = new ArrayList<>();
        //Iterator<Map.Entry<String, List<Department>>> iterator = departmentMap.entrySet().iterator();
        //while (iterator.hasNext()){
        //    Map.Entry<String, List<Department>> listEntry = iterator.next();
        //    //大科室
        //    DepartmentVo departmentVo = new DepartmentVo();
        //    departmentVo.setDepcode(listEntry.getKey());
        //    departmentVo.setDepcode(listEntry.getValue().get(0).getDepname());
        //    //小科室
        //    List<DepartmentVo> children = new ArrayList<>();
        //    listEntry.getValue().forEach(department -> {
        //        DepartmentVo departmentVo2 = new DepartmentVo();
        //        departmentVo2.setDepcode(department.getDepcode());
        //        departmentVo2.setDepname(department.getDepname());
        //        children.add(departmentVo2);
        //    });
        //    departmentVo.setChildren(children);
        //    result.add(departmentVo);
        //}

        List<DepartmentVo> result = new ArrayList<>();
        departmentMap.forEach((bigCode, value) -> {
            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(value.get(0).getDepname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            value.forEach(department -> {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                children.add(departmentVo2);
            });
            departmentVo.setChildren(children);
            result.add(departmentVo);
        });
        return result;
    }

    @Override
    public String getDepName(String hoscode,String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
        if (department!=null){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }
}
